#include <stdio.h>
#include <malloc.h>
#include "LinkList.c"

void mergerList(LinkList la,LinkList lb,LinkList* lc);

int main(){
	int a[]={1,2,5};
	int b[]={2,4,8,10,19};
	LinkList la,lb,lc,e;
	InitList(&la,a,3);
	InitList(&lb,b,5);

    mergerList(la,lb,&lc);
    printf("right\n");
    e=lc->next;
	while(e!=NULL){
		printf("%d\n",e->data);
		e=e->next;
	}
	DestoryList(la);
    DestoryList(lb);
	DestoryList(lc);
}

void mergerList(LinkList la,LinkList lb,LinkList* lc){
	*lc=(LinkList)malloc(sizeof(LNode));
	(*lc)->next=NULL;
	ElemSet ela,elb;
	int i=1 ,a=1,b=1;

	GetElem(la,i,&ela);
	GetElem(lb,i,&elb);
	while(i++){
		if(ela<elb){
			ListInsert(*lc,i,ela);
			if(!GetElem(la,a++,&ela)) break;
		}
		else{
			ListInsert(*lc,i,elb);
			if(!GetElem(lb,b++,&elb)) break;
		}
	}
	if(!GetElem(la,a,&ela)) while(i++ && b++){
		ListInsert(*lc,i,elb);
		if(!GetElem(lb,b,&elb)) break;
	}
	else while(i++ && a++){
		if(!GetElem(la,a,&ela)) break;
		ListInsert(*lc,i,ela);	
	}

}
